package sis.studentinfotest;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-07-26 09:21
 * @notify
 * @version 1.0
 */

import junit.framework.TestCase;
import sis.studentinfo.Student;
import sis.studentinfo.StudentDirectory;

import java.io.IOException;

public class StudentDirectoryTest extends TestCase {
    private StudentDirectory dir;

    //初始化一个学生字典
    protected void setUp() throws IOException {
        dir = new StudentDirectory();
    }

    //删除重置文件
    protected void tearDown() throws IOException {
        dir.close();
        dir.remove();
    }


    public void testStoreAndRetrieve() throws IOException {
        final int numberOfStudents = 10;
        for (int i = 0; i < numberOfStudents; i++) {
            addStudent(dir, i);
        }
        dir.close();
        dir = new StudentDirectory();
        for (int i = 0; i < numberOfStudents; i++) {
            verifyStudentLookup(dir, i);
        }
    }

    //添加学生
    void addStudent(StudentDirectory directory, int i) throws IOException {
        String id = "" + i;
        Student student = new Student(id);
        student.setId(id);
        student.addCredits(i);
        directory.add(student);
    }

    //检查学生
    void verifyStudentLookup(StudentDirectory directory, int i)throws IOException {
        String id = "" + i;
        Student student = dir.findById(id);
        assertEquals(id, student.getLastName());
        assertEquals(id, student.getId());
        assertEquals(i, student.getCredits());
    }
}
